﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using Monkey.Warrenty.Enum;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.User
{
    public partial class ProcessProduct : System.Web.UI.Page
    {
        #region Properties

        private static WarrantyProductBiz _warrantyProductBiz;
        private static WarrantyProductBiz WarrantyProductManager
        {
            get { return _warrantyProductBiz ?? (_warrantyProductBiz = new WarrantyProductBiz()); }
        }

        private long _ticketProductId;
        private long TicketProductId
        {
            get
            {
                if (_ticketProductId <= 0) long.TryParse(Request.Params["id"], out _ticketProductId);
                return _ticketProductId;
            }
        }

        private WarrantyTicketProductView _ticketProductView;
        private WarrantyTicketProductView CurrentTicketProductView
        {
            get
            {
                return _ticketProductView ??
                       (_ticketProductView = WarrantyProductManager.GetWarrantyTicketProductView(TicketProductId));
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (TicketProductId <= 0 || CurrentTicketProductView == null)
                {
                    lblErrorMessage.Text = Resources.GlobalStrings.ProductNotExisted;
                    //btnSave.Enabled = false;
                    return;
                }

                if (WarrantyCommon.CurrrentEmployee.Id != CurrentTicketProductView.EmployeeId)
                    Response.Redirect("Default.aspx");

                switch (CurrentTicketProductView.ReceivedFrom)
                {
                    case 1:
                        CurrentTicketProductView.ReceivedFromName = Resources.GlobalStrings.Customers;
                        break;
                    case 2:
                        CurrentTicketProductView.ReceivedFromName = Resources.GlobalStrings.Employees;
                        CurrentTicketProductView.DealerEmployeeName = CurrentTicketProductView.FromEmployeeName;
                        break;
                    case 3:
                        CurrentTicketProductView.ReceivedFromName = Resources.GlobalStrings.Dealers;
                        CurrentTicketProductView.DealerEmployeeName = CurrentTicketProductView.FromDealerName;
                        break;
                }

                LoadData();
            }
        }

        protected void btnReceivedCompleted_Click(object sender, EventArgs e)
        {
            UpdateProcessProduct();

            WarrantyProductManager.UpdateProductStatus(TicketProductId, WarrantyProductStatus.ReceivedCompleted);

            Response.Redirect("Default.aspx");
        }
        
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateProcessProduct();

            lblErrorMessage.Text = Resources.GlobalStrings.UpdateSuccess;
        }

        #endregion

        #region Helpers

        private void LoadData()
        {
            ltrCode.Text = CurrentTicketProductView.Code;
            ltrName.Text = CurrentTicketProductView.Name;
            ltrModel.Text = CurrentTicketProductView.Model;
            ltrManufacturer.Text = CurrentTicketProductView.ManufacturerName;
            ltrSerial.Text = CurrentTicketProductView.Serial;
            ltrQuantity.Text = CurrentTicketProductView.Quantity.ToString(CultureInfo.InvariantCulture);
            ltrWarrantyType.Text = CurrentTicketProductView.WarrantyTypeName;
            ltrReceivedFromName.Text = CurrentTicketProductView.ReceivedFromName;
            ltrDealerEmployeeName.Text = CurrentTicketProductView.DealerEmployeeName;
            ltrCustomer.Text = CurrentTicketProductView.CustomerName;
            ltrPhone.Text = CurrentTicketProductView.Phone;
            ltrStatus.Text = CurrentTicketProductView.WarrantyProductState;
            txtDescription.Text = CurrentTicketProductView.Description;
            if (CurrentTicketProductView.EmployeeId.HasValue)
            {
                pnlEmployee.Visible = true;
                pnlSupplier.Visible = false;
                // load employees
                var employees = WarrantyProductManager.GetEmployees();
                //employees.Insert(0, new Employee { Id = 0, Name = string.Empty });
                ddlEmployees.DataSource = employees;
                ddlEmployees.DataValueField = "Id";
                ddlEmployees.DataTextField = "Name";
                ddlEmployees.DataBind();
                ddlEmployees.SelectedValue = string.Format("{0}", CurrentTicketProductView.EmployeeId);
            }
            if (CurrentTicketProductView.WarrantySupplierId.HasValue)
            {
                pnlEmployee.Visible = false;
                pnlSupplier.Visible = true;
                // suppliers
                var suppliers = WarrantyProductManager.GetSuppliers();
                //suppliers.Insert(0, new Supplier { Id = 0, Name = string.Empty });
                ddlSuppliers.DataTextField = "Name";
                ddlSuppliers.DataValueField = "Id";
                ddlSuppliers.DataSource = suppliers;
                ddlSuppliers.DataBind();
                ddlSuppliers.SelectedValue = string.Format("{0}", CurrentTicketProductView.WarrantySupplierId);
            }
        }

        private void UpdateProcessProduct()
        {
            CurrentTicketProductView.Description = txtDescription.Text;
            if (CurrentTicketProductView.EmployeeId.HasValue)
            {
                CurrentTicketProductView.EmployeeId = int.Parse(ddlEmployees.SelectedValue);
            }
            if (CurrentTicketProductView.WarrantySupplierId.HasValue)
            {
                CurrentTicketProductView.WarrantySupplierId = int.Parse(ddlSuppliers.SelectedValue);
            }

            WarrantyProductManager.UpdateProcessProduct(CurrentTicketProductView);
        }

        #endregion
    }
}